﻿
using System ;

using MskEdit.Dxf.Model ;
using MskEdit.Sdk ;


namespace MskEdit.Dxf.Backend
{

    public static class DxfBoolReader
    {
        internal static bool? TryParse( string valueStr, int lineNo, bool? fallbackValue ) {
                
            int? maybeInt = SdkValueParser.TryParseToInt( valueStr, null ) ;
            if ( ! maybeInt.HasValue ) {
                SdkDiagnostics.TraceWarning( "Could not parse Int value string.", lineNo ) ;
                return fallbackValue ;
            }
            
            if ( ( maybeInt.Value != 0 ) && ( maybeInt.Value != 1 ) ) {
                SdkDiagnostics.TraceWarning( "Int representing bool is not 0 or 1.", lineNo ) ;
                return fallbackValue ;
            }
                
            
            return maybeInt.Value == 1 ;
        }


        internal static bool? TryRead( string codeStr, string[] allLines, ref int lineNo, bool? fallbackValue ) {
        
            int internalLineNo = lineNo ;

            string valueStr = DxfLineReader.TryRead( codeStr, allLines, ref internalLineNo, null ) ;
            if ( valueStr == null ) {
                SdkDiagnostics.TraceWarning( "Could not read group code and Bool value string.", internalLineNo ) ;
                return fallbackValue ;
            }
            
            bool? maybeBool = TryParse( valueStr, internalLineNo, null ) ;
            if ( ! maybeBool.HasValue )
                return fallbackValue ;
            
            lineNo = internalLineNo ;
            
            return maybeBool ;
        }
        
        
        internal static bool? TryReadAfterCode( string[] allLines, ref int lineNo, bool? fallbackValue )
        {
            int internalLineNo = lineNo ;
            
            string valueStr = DxfLineReader.TryRead( allLines, ref internalLineNo, null ) ;
            if ( valueStr == null ) {
                SdkDiagnostics.TraceWarning( "Could not read Bool value string.", internalLineNo ) ;
                return fallbackValue ;
            }
                 
            bool? maybeBool = TryParse( valueStr, internalLineNo, null ) ;
            if ( ! maybeBool.HasValue )
                return fallbackValue ;
            
            lineNo = internalLineNo ;
            
            return maybeBool ;
        }


        internal static bool? TryRead( string[] allLines, ref int lineNo, bool? fallbackValue ) {
        
            int internalLineNo = lineNo ;
            
            DxfCode dxfCode = DxfCodeReader.TryRead( allLines, ref internalLineNo, null ) ;
            if ( dxfCode == null )
                return fallbackValue ;
            
            if ( ! dxfCode.HasBoolCode )
                return fallbackValue ;
            
            bool? maybeBool = TryReadAfterCode( allLines, ref internalLineNo, null ) ;
            if ( ! maybeBool.HasValue )
                return fallbackValue ;
                
            lineNo = internalLineNo ;
            
            return maybeBool ;
        }

    }
}
